<?php
class Friends_model extends MY_Model
{
	public
		$table = 'users_friends',
		$prKey = 'uf_id',

		$status = array(
			'1' => 'Active',
			'2' => 'Inactive'
		);

	function __construct()
	{
        parent::__construct();

   		$this->fields = array(
			'full_name'			=> T_('Full Name'),
            'email'             => T_('Email'),
            'status'            => T_('Status'),
        );
        $this->rules = array(
            'full_name'			=> "trim|required|max_length[255]",
			'email' 			=> "trim|required|valid_email|max_length[64]|callback___loginFree",
            'status'            => "trim|required|max_length[255]",
        );
    }

    function verifyFriend($fr_code){

    $sql = "UPDATE ".$this->table." 
            SET uf_activation_code='', uf_status='1'
            WHERE uf_activation_code=".$this->db->escape($fr_code)." AND uf_status!=1";
    $result = $this->db->query($sql);
    return $result;

}

	function search($id = 0,$config = array())
	{
        $friend_ID        = $this->input->get_post('uf_ID');
		$full_name   	= $this->input->get_post('uf_full_name');
        $email      = $this->input->get_post('uf_email');
		
		// main sql
		$sql = '';
		$sql .= ' FROM '.$this->table.' u';
		$sql .= ' WHERE 1 = 1';

		if ($friend_ID)
		{
			$sql .= ' AND u.uf_id='.$this->db->escape($friend_ID);
		}

		if ($full_name)
		{
			$sql .= ' AND (u.uf_full_name LIKE ' . $this->db->escape("%" . $full_name . "%").')';
		}

		if ($email)
		{
			$sql .= ' AND u.uf_email='.$this->db->escape($email);
		}

        $sql = "SELECT * " . $sql." AND uf_user_ID=".$id;

        $result = $this->db->query($sql)->result();
        return $result;

	}

  function get($id = null)
	{
		$this->db->from($this->table);
		$this->db->where('uf_id', $id);
		$query = $this->db->get();

		return $query->row_array();
	}

  function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);

		$error = "";
		$report = "";
		foreach ($items as $id)
		{
			$data = $this->friends_model->get($id);

			$res = $this->db->delete($this->table, array($this->prKey => $id));
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. Page was not removed.'));
				$report .= ",0"; // don't deleted
			}
			else
			{
				$this->log->writeMessage('done', T_('Page was removed.'));
				$report .= ",1"; // deleted
			}
		}

		return substr($report, 1);
	}


  //inserts data from array into the fields named as indexes in array
  function insert($data)
  {
    if (!empty($data))
    {
      //$data['friend_created'] = date('Y-m-d H:i:s');
      
      parent::insert($data);

      if ($this->db->insert_id() > 0)
      {
        $this->log->writeMessage('done', T_('User was successfully created.'));
        return TRUE;
      }
      else
      {
        $this->log->writeMessage('error', T_('Error! User was not created.'));

        return FALSE;
      }
    }
  }

  function update($data)
  {
    if (!empty($data))
    {

     $res = parent::update($data);

      $this->log->writeMessage('done', T_('The data saved successfully.'));

      return TRUE;
    }
  }

  	//gets user information according to field
    function getUserField($field){
        $userID=$this->auth->getID();
    	$sql = "SELECT ".$field."
    			FROM ".$this->table." 
    			WHERE user_ID='".$userID."'";
        $result = $this->db->query($sql)->result_array();
        return $result;
    }
     function get_id(){
    $sql = "SELECT MAX(uf_id) AS number
            FROM ".$this->table;
    $id = $this->db->query($sql)->row_array();
    return $id['number']+1;
    }
    function searchByID($id){
        $sql = "SELECT *
    			FROM ".$this->table." 
    			WHERE uf_user_ID='".$id."'";
        $result = $this->db->query($sql)->result();
        return $result;
    }
}

?>